<template>
 <div class="categories">
  <el-button plain type='success' class="addbtn" @click='showadddialog'>添加分类</el-button>
  <!-- 表格 -->
  <el-table
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    v-loading='isLoading'
    row-key='cat_id'
    :data='categoryList'>
    <el-table-column label="分类名称" prop='cat_name'></el-table-column>
    <el-table-column label="是否有效">
      <template v-slot:default='{row}'>
          {{ row.cat_deleted ? '否':'是' }}
      </template>
    </el-table-column>
    <el-table-column label="层级" prop='cat_level'></el-table-column>
    <el-table-column label="操作">
      <template v-slot:default='{row}'>
        <el-button size='small' type='primary' plain icon='el-icon-edit'></el-button>
        <el-button size='small' type='danger' plain icon='el-icon-delete'></el-button>
      </template>
    </el-table-column>
  </el-table>

    <!-- 分页 -->
   <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 11, 13, 14]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addVisible"
      width="40%">

     <el-form :rules="rules" ref='form' :model='form' label-width="80px">
       <el-form-item label="分类名称" prop='cat_name'>
          <el-input v-model="form.cat_name" placeholder="请输入分类名称"></el-input>
       </el-form-item>
       <el-form-item label='父级名称' prop='cat_pid'>
         <el-cascader
         ref="cascader"
          clearable
          v-model="form.cat_pid"
          :options="options"
          :props="props"></el-cascader>
       </el-form-item>
     </el-form>

     <template v-slot:footer>
        <span class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addcategories">确 定</el-button>
      </span>
     </template>
    </el-dialog>
 </div>
</template>

<script>
export default {
  data () {
    return {
      pagenum: 1,
      pagesize: 5,
      categoryList: [],
      total: 0,
      isLoading: false,
      addVisible: false,
      form: {
        cat_pid: [], // 和级联选择器绑定，数据双向绑定，选择ID，会以数组展示
        cat_name: '', // 分类名称
        cat_level: ''
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] }
        ]
      },
      options: [],
      props: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  created () {
    this.getCategoryList()
  },
  methods: {
    async getCategoryList () {
      this.isLoading = true
      // 发送ajax，get请求的参数
      const { meta, data } = await this.$axios.get('categories', {
        params: {
          type: 3,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      })
      if (meta.status === 200) {
        this.categoryList = data.result
        this.total = data.total
      }
      this.isLoading = false
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.pagesize = 1
      this.getCategoryList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.pagenum = val
      this.getCategoryList()
    },
    async showadddialog () {
      this.addVisible = true
      // 发请求，获取一二级数据
      const { meta, data } = await this.$axios.get('categories?type=2')
      if (meta.status === 200) {
        this.options = data
      } else {
        this.$message.error(meta.msg)
      }
    },
    async addcategories () {
      console.log(123)
      try {
        // 表单校验
        await this.$refs.form.validate()
        const { cat_pid: catPid } = this.form
        // 发请求
        const { meta } = await this.$axios.post('categories', {
          // 父id是数组的最后一项，如没有，默认为0

          cat_pid: catPid[catPid.length - 1] || 0,
          cat_name: this.form.cat_name,
          cat_level: catPid.length
        })
        console.log(meta)
        if (meta.status === 201) {
          // 提示消息
          this.$message.success(meta.msg)
          // 隐藏对话框
          this.addVisible = false
          // 重置表单
          this.$refs.form.resetFields()
          // 重新渲染
          this.getCategoryList()
        } else {
          this.$message.error(meta.msg)
        }
      } catch (e) {
        console.log(e)
      }
    }
  },
  watch: {
    'form.cat_pid' (newValue) {
      this.$refs.cascader.dropDownVisible = false
    }
  }
}
</script>

<style lang ='less' scoped>
.addbtn {
  margin-bottom: 10px;
}
</style>
